<?php
/**
 * Joomla! 1.5 component JoomGPSTracks
 *
 * @version $Id: config.php 2009-06-24 15:40:19 svn $
 * @author Michael Pfister
 * @package joomGPStracks
 * @subpackage backend
 * @license GNU/GPL
 * @filesource
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Import Joomla! libraries
jimport('joomla.application.component.model');
/**
 * Model Class Configuration
 */
class JoomgpstracksModelConfig extends JModel  {
    /**
     * 
     */
    function __construct() {
		parent::__construct();
    }
    /**
     *
     * @return boolean
     */
    function saveConfig()  {

        // get post data
        $row =& JRequest::get( 'post' );
        $table = $this->getTable( 'gps_config' );
        $table->bind( $row );

        if (!$table->store()) {
            return JError::raiseWarning( $table->getError() );
        }
        return TRUE;

    }
    /**
     *
     * @global object $mainframe
     * @return array
     */
    function getContent()  {
        global $mainframe;

        $db =& JFactory::getDBO();

        $query = "SELECT id, title FROM #__content WHERE"
                . "\n sectionid='0'"
                . "\n AND catid='0'"
                . "\n AND state='1'";

        $db->setQuery($query);
        $result = $db->loadObjectList();

        if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}

        return $result;
    }

    /**
     *
     * @global object $mainframe
     * @return boolean
     */
    function installCleanDB()  {
        global $mainframe;

        $db =& JFactory::getDBO();

        $file = JPath::clean(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_joomgpstracks'.DS.'sql'.DS.'clean_install.sql');
        $buffer = file_get_contents($file);

        if($buffer === false) {
            return false;
        } else {
            jimport('joomla.installer.helper');
            $queries = JInstallerHelper::splitSql($buffer);

            foreach($queries as $query)  {
                $query = trim($query);
                if($query != '' && $query != '#') {
                    $db->setQuery($query);
                    $db->query();
                }
            }
		return true;
        }
        
    }

    /**
     *
     * @global object $mainframe
     * @return boolean
     */
    function updateDB()  {
        global $mainframe;

        $db =& JFactory::getDBO();

        $file = JPath::clean(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_joomgpstracks'.DS.'sql'.DS.'update.sql');
        $buffer = file_get_contents($file);

        if($buffer === false) {
            return false;
        } else {
            jimport('joomla.installer.helper');
            $queries = JInstallerHelper::splitSql($buffer);

            foreach($queries as $query)  {
                $query = trim($query);
                if($query != '' && $query != '#') {
                    $db->setQuery($query);
                    $db->query();
                }
            }
		return true;
        }

    }

    function getTemplates()  {
        jimport('joomla.filesystem.file');

        $templates = JFolder::listFolderTree('../components/com_joomgpstracks/assets/template','',1);
        return $templates;
    }
}
?>
